var common = require("../../public.js");
Page({
  data: {
    pageNum: 1,
    pageSize: 10,
    storeList: [],
    type: '',
    isLoadingBusy: false,
    noMoreData: false,
    city: '',
    lon: 0,
    lat: 0,
    refreshing: false
  },
  onLoad: function (options) {
    const that = this
    const id = options.id
    that.setData({
      id,
      type: options.type
    })
    const name = options.name
    if (name) {
      wx.setNavigationBarTitle({
        title: name,
      })
    }

    getApp().getLocation(function (data) {
      that.data.lon = data.longitude
      that.data.lat = data.latitude
      that.data.city = data.regeocodeData.addressComponent.city
      that.setData({
        refreshing: true
      })
      // wx.startPullDownRefresh()
      that.getViewPointList(true)
    }, function () {
      that.setData({
        refreshing: true
      })
      // wx.startPullDownRefresh()
      that.getViewPointList(true)
    })
  },

  getViewPointList(refresh = false) { // 获取景点列表
    const that = this
    if (refresh) {
      that.setData({
        noMoreData: false
      })
    }
    if (that.data.noMoreData || that.data.isLoadingBusy) {
      return
    }
    if (refresh) {
      that.setData({
        refreshing: true
      })
    } else {
      this.setData({
        isLoadingBusy: true
      })
    }
    const pageNum = refresh ? 1 : this.data.pageNum + 1

    let tempJson = {
      storeTypeId: that.data.id,
      cityName: that.data.city,
      pageNum,
      pageSize: that.data.pageSize,
      xpoint: that.data.lon,
      ypoint: that.data.lat,
      distance: '',
      recentIncome: '',
      commentRate: '',
    }
    common.post('/store/getSearchStoreListByStoreTypeId', tempJson, function (res) {
      if (res.status === 200) {
        that.data.pageNum = pageNum
        if (res.data != '') {
          const list = refresh ? res.data : [...that.data.storeList, ...res.data]
          that.setData({
            storeList: list
          })
        } else {
          that.setData({
            noMoreData: true
          })
        }
      }
    }, function () {
      that.setData({
        isLoadingBusy: false,
        refreshing: false
      })
      if (refresh) {
        //停止下拉刷新
        wx.stopPullDownRefresh()
      }
    })
  },
  toViewPoint(e) {
    let {
      id
    } = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/ScenicArea/ViewPointMainPage/index?id=' + id + '&moduleType=' + this.data.type,
    })
  },
  onReachBottom: function () {
    this.getViewPointList(false)
  },

  onPullDownRefresh: function () {
    this.getViewPointList(true)
  },
})